package guoanmaker.operator.business.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import guoanmaker.operator.business.model.OperatorComplaintType;
import guoanmaker.operator.business.model.OperatorDepartment;
import guoanmaker.operator.business.model.OperatorNewsType;
import guoanmaker.operator.business.model.OperatorQuestionOrder;
import guoanmaker.operator.business.model.OperatorQuestionOrderLog;
import guoanmaker.operator.business.model.OperatorUser;
import guoanmaker.operator.business.model.PerviderDictionary;
import guoanmaker.operator.business.repository.OperatorComplaintTypeRepository;
import guoanmaker.operator.business.repository.OperatorDepartmentRepository;
import guoanmaker.operator.business.repository.OperatorQuestionOrderLogRepository;
import guoanmaker.operator.business.repository.OperatorQuestionOrderRepository;
import guoanmaker.operator.business.repository.OperatorUserRepository;
import guoanmaker.operator.business.repository.ProviderDictionaryRepository;

@Service
public class OperatorQuestionOrderService implements OperatorQuestionOrderServiceInterface {
	@PersistenceContext
	private EntityManager manager;
	@Autowired
	private OperatorQuestionOrderRepository operatorQuestionOrderRepository;
	@Autowired
	private OperatorQuestionOrderLogRepository operatorQuestionOrderLogRepository;
	@Autowired
	private OperatorUserRepository operatorUserRepository;
	@Autowired
	private  ProviderDictionaryRepository providerDictionaryRepository;
	@Autowired
	private OperatorDepartmentRepository operatorDepartmentRepository;
	@Autowired
	private OperatorComplaintTypeRepository operatorComplaintTypeRepository;
	
	@Override
	@Transactional
	public OperatorQuestionOrder saveOperatorQuestionOrder(OperatorQuestionOrder operatorQuestionOrder) {
		OperatorUser operatorUser=operatorUserRepository.getOne(operatorQuestionOrder.getLoginid());
		operatorQuestionOrder.setLoginid(operatorUser.getName());
		if(operatorQuestionOrder.getOrderStatus().equals("4")){
			operatorQuestionOrder.setAcceptUsername(operatorUser.getName());
			operatorQuestionOrder.setAcceptDatetime(new Date());
			operatorQuestionOrder.setDoUserName(operatorUser.getName());
			operatorQuestionOrder.setCloseDatetime(new Date());
		}
		operatorQuestionOrder.setSwitchtoDatetime(new Date());
		SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); 
		Date date = new Date(); 
		String dateString = "GD"+bartDateFormat.format(date);
		operatorQuestionOrder.setComplaintNum(dateString);
		operatorQuestionOrder.setCreateTime(date);
		OperatorQuestionOrder questionorder=operatorQuestionOrderRepository.save(operatorQuestionOrder);
		OperatorQuestionOrderLog operatorQuestionOrderLog=new OperatorQuestionOrderLog();
		operatorQuestionOrderLog.setQuestionOrderId(questionorder.getId());
		operatorQuestionOrderLog.setCreateTime(date);
		OperatorDepartment operatorDepartment=questionorder.getDepartmentId();
		String departmentId=null;
		if(operatorDepartment!=null){
			departmentId=questionorder.getDepartmentId().getId();
		}
		operatorQuestionOrderLog.setDepartmentName(departmentId);
		if(operatorQuestionOrder.getOrderStatus().equals("4")){
			operatorQuestionOrderLog.setDisposeDoway("管理员关闭");
		}
		else{
			operatorQuestionOrderLog.setDisposeDoway("管理员转派");
		}
		operatorQuestionOrderLog.setDisposeOpinion(questionorder.getDisposeOpinion());
		operatorQuestionOrderLog.setLoginName(operatorUser.getName());
		operatorQuestionOrderLogRepository.save(operatorQuestionOrderLog);
		return questionorder;
	}
	
	/**
	 * 全部分页列表
	 */
	  @Override
	    public Page<OperatorQuestionOrder> getOperatorQuestionOrderBytime(Integer page, Integer size) {
	        return operatorQuestionOrderRepository.findAll(new PageRequest(page, size, new Sort(Sort.Direction.DESC, "createTime")));
	    }
	  
	  @Override
	    public Page<OperatorQuestionOrder> getOperatorQuestionOrderByorderStatus(Integer page, Integer size,String orderStatus) {
	        return operatorQuestionOrderRepository.findByorderStatus(orderStatus,new PageRequest(page, size, new Sort(Sort.Direction.DESC, "createTime")));
	    }

	@Override
	public Page<OperatorQuestionOrder> getOperatorQuestionOrderBytimeAnddepartmentId(Integer page, Integer size,
			String departmentId) {
		// TODO Auto-generated method stub
		return operatorQuestionOrderRepository.findBydepartmentId(departmentId, new PageRequest(page, size, new Sort(Sort.Direction.DESC, "createTime")));
	}

	@Override
	public Page<OperatorQuestionOrder> getOperatorQuestionOrderBytimeAnddepartmentIdAndStatus(Integer page,
			Integer size, String departmentId, String orderStatus) {
		// TODO Auto-generated method stub
		return operatorQuestionOrderRepository.findByorderStatusAnddepartmentId(orderStatus, departmentId, new PageRequest(page, size, new Sort(Sort.Direction.DESC, "createTime")));
	}

	@Override
	public Map<String,Object> selectOperatorQuestionOrderById(String id) {
		// TODO Auto-generated method stub
		Map<String, Object> reMap = new HashMap<String, Object>();
		OperatorQuestionOrder od=operatorQuestionOrderRepository.findOne(id);
		//OperatorUser operatorUser=operatorUserRepository.findOne(od.getLoginid());
		//od.setLoginid(operatorUser.getName());
		List<OperatorQuestionOrderLog> odlogs=operatorQuestionOrderLogRepository.findByquestionOrderId(id);
		for(OperatorQuestionOrderLog odlog:odlogs){
			String ids=odlog.getDepartmentName();
			if(ids!=null&&ids.equals("")){
				OperatorDepartment operatorDepartment=operatorDepartmentRepository.findOne(ids);
				if(operatorDepartment!=null)
					odlog.setDepartmentName(operatorDepartment.getDepname());
			}
			else{
				odlog.setDepartmentName("");
			}
		}
		reMap.put("od", od);
		reMap.put("odlogs", odlogs);
		return  reMap;
	}

	@Transactional
	public int updateOperatorQuestionOrder(Integer stauts,String userid,String id,String orderStatus,String departmentId,
			String disposeOpinion,String disposeWay,String refund,Double money) {
		//部门工单的待处理工单中的确认接单操作 状态码
		OperatorUser operatorUser=operatorUserRepository.getOne(userid);
		String name=operatorUser.getName();
		int a=0;
		if(stauts==1){
			String jpql="update  OperatorQuestionOrder r set r.orderStatus=:orderStatus,"
					+ "r.acceptDatetime=:acceptDatetime,r.acceptUsername=:acceptUsername where r.id=:id";
			Query query=manager.createQuery(jpql).setParameter("orderStatus", orderStatus)
					.setParameter("id", id).setParameter("acceptDatetime", new Date()).setParameter("acceptUsername",name);
			a=query.executeUpdate();
		}
		//部门工单的待处理工单中的转派操作 状态码
		else if(stauts==2){
			String jpql="update  OperatorQuestionOrder r set r.departmentId.id=:departmentId,r.switchtoDatetime=:switchtoDatetime"
					+ " where r.id=:id";
			Query query=manager.createQuery(jpql).setParameter("departmentId", departmentId).setParameter("switchtoDatetime", new Date())
					.setParameter("id", id);
			a=query.executeUpdate();
		}
		//部门工单的   处理中工单  状态码
		else if(stauts==3){
			String jpql=null;
			Query query=null;
			if(orderStatus.equals("1")){
				jpql="update  OperatorQuestionOrder r set r.departmentId.id=:departmentId,r.switchtoDatetime=:switchtoDatetime,"
						+ "r.orderStatus=:orderStatus,r.disposeOpinion=:disposeOpinion where r.id=:id";
				query=manager.createQuery(jpql).setParameter("departmentId", departmentId).setParameter("switchtoDatetime", new Date())
						.setParameter("orderStatus", orderStatus).setParameter("disposeOpinion", disposeOpinion).setParameter("id", id);
			}
			else{
				jpql="update  OperatorQuestionOrder r set r.doUserName=:doUserName,"
						+ "r.orderStatus=:orderStatus,r.refund=:refund,r.money=:money where r.id=:id";
				query=manager.createQuery(jpql)
						.setParameter("doUserName", name).setParameter("orderStatus", orderStatus)
						.setParameter("refund", refund).setParameter("money", money).setParameter("id", id);
			}
			a=query.executeUpdate();
		}
		//管理员工单中的待确认关闭工单   ————————同意操作
		else if(stauts==4){
			String jpql="update  OperatorQuestionOrder r set r.closeDatetime=:closeDatetime,"
					+ "r.orderStatus=:orderStatus where r.id=:id";
			Query query=manager.createQuery(jpql).setParameter("closeDatetime", new Date())
					.setParameter("orderStatus", orderStatus).setParameter("id", id);
			a=query.executeUpdate();
		}
		//管理员工单中的待确认关闭工单   ————————转派操作
		else if(stauts==5){
			String jpql="update  OperatorQuestionOrder r set r.departmentId.id=:departmentId,"
					+ "r.orderStatus=:orderStatus,r.switchtoDatetime=:switchtoDatetime where r.id=:id";
			Query query=manager.createQuery(jpql).setParameter("departmentId", departmentId).setParameter("switchtoDatetime", new Date())
					.setParameter("orderStatus", orderStatus).setParameter("id", id);
			a=query.executeUpdate();
		}
		OperatorQuestionOrderLog operatorQuestionOrderLog=new OperatorQuestionOrderLog();
		operatorQuestionOrderLog.setCreateTime(new Date());
		operatorQuestionOrderLog.setDepartmentName(departmentId);
		operatorQuestionOrderLog.setDisposeDoway(disposeWay);
		operatorQuestionOrderLog.setDisposeOpinion(disposeOpinion);
		operatorQuestionOrderLog.setLoginName(name);
		operatorQuestionOrderLog.setQuestionOrderId(id);
		operatorQuestionOrderLogRepository.save(operatorQuestionOrderLog);
		return a;
	}
	public List<OperatorComplaintType> getComplaintType(){
		return operatorComplaintTypeRepository.findAll();
	}
	public List<OperatorDepartment>  findByStauts(){
		List<OperatorDepartment>  list=operatorDepartmentRepository.findByStauts(0);
		return list;
	}
}
